package com.pfzy.utildemo.activity.im;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.pfzy.utildemo.R;
import com.pfzy.utildemo.activity.base.BaseActivity;
import com.pfzy.lib.config.log.MyLog;
import com.pfzy.utildemo.databinding.ActivityImBinding;

/**
 * https://juejin.cn/post/6991429231821684773
 * https://juejin.cn/post/7028397244894330917
 * https://juejin.cn/post/7208023562807459896
 * https://juejin.cn/post/7211437215335743548
 * https://juejin.cn/post/7209950095403581496
 * https://juejin.cn/post/7224015103481200677
 * https://juejin.cn/post/7083017803388682270
 * https://juejin.cn/post/7023181690130415630
 * https://juejin.cn/post/6844903616659062792
 * https://juejin.cn/post/7117074935834476574
 * <p>
 * https://gitee.com/explore/im?lang=Java
 * https://gitee.com/wfchat/android-chat/tree/master
 */
public class IMActivity extends BaseActivity implements ChatContract.View {
    private ActivityImBinding mBind;
    private ChatPresenter presenter = new ChatPresenter(this);
    private InputMethodManager inputMethodManager;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mBind = ActivityImBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());

        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        initView();
    }

    private void initView() {
        mBind.recyclerView.setHasFixedSize(true);
        mBind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBind.recyclerView.setAdapter(new MessageListAdapter2(this, presenter.messages));

        mBind.edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 如果用户输入的文本长度大于0，发送按钮开启
                if (s.length() > 0) {
                    mBind.send.setEnabled(true);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 如果用户输入的文本长度大于0，发送按钮开启
                if (s.length() > 0) {
                    mBind.send.setEnabled(true);
                }
            }
        });
        mBind.edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                send();
                return true;
            }
        });

        mBind.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });
    }

    private void send() {
        hideSoftKeyboard();
        String send = mBind.edit.getText().toString();
        String message = send.trim();

        presenter.sendMessage("自己", message);
    }

    /**
     * 隐藏软键盘
     */
    private void hideSoftKeyboard() {
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }

    /**
     * 为了展示发送进度条而刷新
     */
    @Override
    public void onStartSendMessage() {
        _log("发送消息"+presenter.messages.toString());
        mBind.recyclerView.getAdapter().notifyDataSetChanged();
    }

    /**
     * 为了展示发送消息item
     */
    @Override
    public void onSendMessageSuccess() {
        mBind.recyclerView.getAdapter().notifyDataSetChanged();
        mBind.edit.setText("");
        Toast.makeText(this, R.string.send_message_success, Toast.LENGTH_LONG).show();
    }

    /**
     * 为了展示发送消息失败的叹号
     */
    @Override
    public void onSendMessageFailed() {
        mBind.recyclerView.getAdapter().notifyDataSetChanged();
        Toast.makeText(this, R.string.send_message_failed, Toast.LENGTH_LONG).show();
    }

    private void _log(String msg) {
        MyLog.logd("即时通讯: " + msg);
    }
}
